
--TriggerTypes={
--    Once =1,
--    Multiple=2,
--    Life=3
--}

local Trigger = class("Trigger",function()
    return cc.Node:create()
end)

--Trigger.STATE_NONE = 0
Trigger.STATE_ENTER = 1--进入状态
Trigger.STATE_IN = 2--触发中状态
Trigger.STATE_OUT = 3--退出状态

function Trigger:ctor()
    
    self.mX = 0
    self.mWidth = 0
    
    self.mState = Trigger.STATE_ENTER
    
    self:scheduleUpdate( handler(self, self.updateFrame))
    
end

function Trigger:updateFrame(dt)
    if self.mTargetCharacter then
        local x = self.mTargetCharacter:getPositionX()
        if x >= self.mX and x <= self.mX + self.mWidth then
            --在区间中
            --print("Trigger:isInTheInterval  在区间中")
            if self.mState == Trigger.STATE_ENTER then
                self.mState = Trigger.STATE_IN
                self.mOnEnterDelegate()
            elseif self.mState == Trigger.STATE_IN then
                self.mOnIntervalDelegate()
            end
           
        else
            --不在区间中
            if self.mState == Trigger.STATE_IN then
                self.mState = Trigger.STATE_ENTER
                self.mOnExitDelegate()
            end
            --print("Trigger:isInTheInterval  不在区间中")
       
        end
    end      
end

--[[--
-- 设置触发区间
--ival.x 区间x坐标
--ival.w 区间宽度
]]
function Trigger:setInterval(ival)
    self.mX = ival.x
    self.mWidth = ival.w
end

--[[--
-- 设置触发目标角色
]]
function Trigger:setTarget(character)
    self.mTargetCharacter = character
end

--[[--
-- 设置进入区间回调委托
]]
function Trigger:setOnEnterDelegate(delegate)
    self.mOnEnterDelegate = delegate
end

--[[--
-- 设置在区间中回调委托
]]
function Trigger:setOnIntervalDelegate(delegate)
    self.mOnIntervalDelegate = delegate
end

--[[--
-- 设置退出区间回调委托
]]
function Trigger:setOnExitDelegate(delegate)
    self.mOnExitDelegate = delegate
end

--function Trigger.create(aabbBox)
--    local trigger = Trigger.new()
--    trigger:init()  
--    trigger:setAABB(aabbBox)
--    return trigger
--end
--
--function Trigger:ctor()
--    self.type = TriggerTypes.Once
--    self.isTriggered = false
--end
--
--function Trigger:init()
--    cclog("Trigger init")
--end
--
--
--function Trigger:setType(type)
--    self.type = type
--end
--
--function Trigger:setAABB(aabbBox)
--    self.aabbBox = aabbBox
--end
--

--

--
--function Trigger:update(targetPos)
--    if self.isTriggered then
--        if not self:isInside(targetPos) then
--            self.isTriggered = false;
--            self.onExitDelegate()
--        end
--    else
--        if self:isInside(targetPos) then
--            self.isTriggered = true;
--            self.onEnterDelegate()
--        end
--    end
--end

return Trigger